package com.tiantianyuegou.util;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by ivany on 2016/1/15.
 */
public class TimeUtil {

    private static SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static Long endTime;

    /**
     * 初始化倒计时
     *
     * @param endDate
     */
    public static void initCountDown(String endDate) {
        try {
            endTime = df.parse(endDate).getTime();// 结束时间
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /***
     * 执行倒计时
     *
     * @return
     */
    public static String execCountDown() {
        Date now = new Date(System.currentTimeMillis());// 当前时间
        long l = endTime - now.getTime();
        long day = l / (24 * 60 * 60 * 1000);
        long hour = (l / (60 * 60 * 1000) - day * 24);
        long min = ((l / (60 * 1000)) - day * 24 * 60 - hour * 60);
        long sec = (l / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
        StringBuffer time = new StringBuffer();
        if (day < 10) {
            time.append("0").append(day);
        } else {
            time.append(day);
        }
        time.append(":");
        if (hour < 10) {
            time.append("0").append(hour);
        } else {
            time.append(hour);
        }
        time.append(":");
        if (min < 10) {
            time.append("0").append(min);
        } else {
            time.append(min);
        }
        time.append(":");
        if (sec < 10) {
            time.append("0").append(sec);
        } else {
            time.append(sec);
        }
        return time.toString();
    }
}

